<template>
	<div class="content">

		<h1 style="text-align: center;">分销海报</h1>

		<div class="poster">
			<van-image
				width="100%"
				height="100%"
				fit="cover"
				:src="poster"
			/>
		</div>

		<van-cell-group inset>
			<van-field
				v-model="link"
				center
				clearable
				readonly
			>
				<template #button>
					<van-button size="small" type="primary" @Click="Copy">复制链接</van-button>
				</template>
			</van-field>
		</van-cell-group>
			
    
		<div class="button-wrapper">
			<van-button type="danger" style="width: 55%;" @Click="Poster">
				<van-swipe
					vertical
					class="notice-swipe"
					:autoplay="1500"
					:touchable="false"
					:show-indicators="false">
          <van-swipe-item class="word">点击重新生成海报</van-swipe-item>
					<van-swipe-item class="word">做任务</van-swipe-item>
					<van-swipe-item class="word">得奖金</van-swipe-item>
				</van-swipe>
			</van-button>
		</div>
    
		
		<Tabbar />
	</div>
</template>

<script setup>

  const {proxy} = getCurrentInstance()
  let business = proxy.$business.value
  console.log(business)
	let link = ref()
  let poster = ref()

  onMounted(() => {
    PosterData()
  })

  const PosterData = async () =>
  {
    var params = {
      busid: business.id,
      mobile: business.mobile,
    }
    var result = await proxy.$POST({
      url: 'sale/index',
      params
    })

    if(result.code == 0)
    {
      proxy.$fail(result.msg)
      return false
    }

    link.value=result.data.link

    poster.value=result.data.poster


  }

  const PosterUpdate = async () =>
  {
    var params = {
      busid: business.id,
      mobile: business.mobile,
      avatar: business.avatar_text,
      nickname: business.nickname
    }
    var result = await proxy.$POST({
      url: 'sale/poster',
      params
    })

    if(result.code == 0)
    {
      proxy.$fail(result.msg)
      return false
    }
    
    link.value=result.data.link

    //可以通过给海报后面加随机数来刷新图片
    poster.value=result.data.poster + '?id=' + Math.random()

    // 推荐地址存储到本地
    proxy.$link.value = result.data.link

    // proxy.$success({
    //   message: result.msg,
    //   onClose: () => {
    //     proxy.$router.go(0)
    //   }
    // })
  }

  const Poster = async () =>
  {
    proxy.$success({
      message:'更新海报成功',
      onClose:() =>
      {
        PosterUpdate()
      }
    })
  }
  const Copy = async () =>
  {
    console.log(123123)
  }
</script>

<style scoped>
  @import url('/assets/css/SaleMoney.css');
  ::v-deep .van-cell{
    padding: 0;
  }

</style>